$(function(){
	findType();
	findFirm();
});
function findType(){
	$("#typeSelect").html("");
	$("#typeId").html("");
	var url="../../matype/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#typeId").append("<option value=''>====请选择====</option>");
		$("#typeSelect").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#typeId").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			$("#typeSelect").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			load();
		});
	},"json");
}
function findFirm(){
	$("#firmId").html("");
	var url="../../firm/query.action";
	var data={"page":1,"limit":100};
	$.post(url,data,function(mes){
		$("#firmId").append("<option value=''>====请选择====</option>");
		$.each(mes.data,function(index,item){
			$("#firmId").append("<option value="+item.firmId+">"+item.firmName+"</option>");
			load();
		});
	},"json");
}

function exit(){
	$("#maId").val("");
	$("#maNameInput").val("");
	$("#maMoney").val("");
	$("#maUnit").val("");
	$("#firmId").val("");
	$("#typeId").val("");
	load();
	//关闭所有页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table'], function(){
	var table = layui.table
	,laydate = layui.laydate
	,form = layui.form
	,layer = layui.layer
	,layedit = layui.layedit;

	//日期
	laydate.render({
		elem: '#date'
	});

	laydate.render({
		elem: '#time'
	});
	//监听工具条
	table.on('tool(demo)', function(obj){
		var da = obj.data;

		if(obj.event === 'edit'){
			var url="../../materials/findById.action";
			var data={"maId":da.maId};
			$.post(url,data,function(mes){
				layer.open({
					type: 1	 //此处以iframe举例
					,title: '数据修改'
						,area: ['600px', '450px']
				,offset: 'auto'
					,content:$('#addOrUpdate')
					,success: function(layero){
						$("#maId").val(mes.maId);
						$("#maNameInput").val(mes.maName);
						$("#maMoney").val(mes.maMoney);
						$("#maUnit").val(mes.maUnit);
						$("#firmId").val(mes.firmId);
						$("#typeId").val(mes.typeId);
						//渲染
						load();
					},end : function() {
						//弹框关闭的回调函数
						$("#addOrUpdate").hide();
						exit();
					}
				});

			},"json");
		}else if(obj.event === 'del'){
			layer.confirm('真的删除行么', function(index){
				var index = layer.load(0, {shade: false});
				var url="../../materials/delete.action";
				var data={"maId":da.maId}
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						obj.del();
						layer.close(index);
						layer.msg("删除成功");
					}else{
						layer.msg("删除失败");
					}
				},"json");

			});
		}

	});
	var $ = layui.$, active = {
			reload: function(){
				var date = $('#date');
				var time = $('#time');
				var maName = $('#maName');
				var typeSelect = $('#typeSelect');
				//执行重载
				table.reload('idTest', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				,where: {
					date: date.val(),
					time: time.val(),
					maName: maName.val(),
					typeId: typeSelect.val(),
				}
				});
			}
	};

	$('.demoTable .layui-btn').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	$('.demoTable #insert').on('click', function(){
		layer.open({
			type: 1	 //此处以iframe举例
			,title: '增加类型'
				,area: ['600px', '450px']
		,offset: 'auto'
			,content:$('#addOrUpdate')
			,end : function() {
				$("#operUser").attr({"disabled":false});
				//弹框关闭的回调函数
				$("#addOrUpdate").hide();
				exit();
			}
		});
	});

	$('#sub').on('click', function(){
		findMaUnit();
		findMaMoney();
		findMaName();

		var countyz="";
		$.each($(".clyz"),function(index,item){
			if($(item).val()=="√"){
				countyz++;
			}
		});
		if(countyz==3){
			$("#myform").submit();

			/*var url="../../materials/addOrUpdate.action"; 
					 var data=$("#myform").serialize();
					 $.post(url,data,function(mes){
						 if(mes==1){
							 exit();
							 $("#addOrUpdate").hide();
							 parent.layer.msg('操作成功', {
							      icon: 1
							  });
							//重载表单
		 					active['reload'] ? active['reload'].call(this) : '';
						 }else{
							 parent.layer.msg('操作失败', {
							      icon: 2
							  });
						 }
					 },"json");*/
		}

	});

});
function findMaName(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;

		if($("#maNameInput").val()=="" || $("#maNameInput").val()==null ){
			parent.layer.msg('材料名称不能为空', {
				icon: 5,
				anim :6
			});
			$("#maNameInputyz").val("×");
		}else if($("#maNameInput").val().length>10){
			parent.layer.msg('材料名称长度不能超过10', {
				icon: 5,
				anim :6
			});
			$("#maNameInputyz").val("×");
		}else{
			$("#maNameInputyz").val("√");
		}

	});
}
function findMaMoney(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;

		if($("#maMoney").val()=="" || $("#maMoney").val()==null ){
			parent.layer.msg('材料价格不能为空', {
				icon: 5,
				anim :6
			});
			$("#maMoneyInputyz").val("×");
		}else if(isNaN($("#maMoney").val())){
			parent.layer.msg('材料价格必须是数字', {
				icon: 5,
				anim :6
			});
			$("#maMoneyInputyz").val("×");
		}else{
			$("#maMoneyInputyz").val("√");
		}

	});
}
function findMaUnit(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;

		if($("#maUnit").val()=="" || $("#maUnit").val()==null ){
			parent.layer.msg('材料单位不能为空', {
				icon: 5,
				anim :6
			});
			$("#maUnitInputyz").val("×");
		}else if($("#maUnit").val().length>10){
			parent.layer.msg('材料单位长度不能超过10', {
				icon: 5,
				anim :6
			});
			$("#maUnitInputyz").val("×");
		}else{
			$("#maUnitInputyz").val("√");
		}
	});
}

function load() {
	var form = layui.form
	form.render();
}

function ss(){
	var firmId=document.getElementById("firmId");
	if(firmId.value==""){
		parent.layer.msg('请选择生产厂商', {
			icon: 5,
			anim :6
		});
		return false;
	}
	var typeId=document.getElementById("typeId");
	if(typeId.value==""){
		parent.layer.msg('请选择材料类型', {
			icon: 5,
			anim :6
		});
		return false;
	}
	return true;
}
